package com.jeasonzhao.commons.inplacecache;

public class CacheObject<K2,V2>
{
    private final K2 key;
    private final V2 cachedObject;
    private long lastAccess = 0L; // time of last access
    private int accessCount = 0; // number of accesses
    private long ttl = 0L; // objects timeout (time-to-live), 0 = no timeout

    public CacheObject(K2 key,V2 object,long ttl)
    {
        this.key = key;
        this.cachedObject = object;
        this.ttl = ttl;
        this.lastAccess = System.currentTimeMillis();
    }

    public boolean isExpired()
    {
        if(ttl == 0)
        {
            return false;
        }
        else
        {
            return lastAccess + ttl < System.currentTimeMillis();
        }
    }

    public K2 getKey()
    {
        return this.key;
    }

    public V2 getObject()
    {
        lastAccess = System.currentTimeMillis();
        accessCount++;
        return cachedObject;
    }

    public int getAccessCount()
    {
        return accessCount;
    }

    public long getLastAccess()
    {
        return lastAccess;
    }

    public long getTtl()
    {
        return ttl;
    }

    public void setAccessCount(int accessCount)
    {
        this.accessCount = accessCount;
    }

    public void setLastAccess(long lastAccess)
    {
        this.lastAccess = lastAccess;
    }

    public void setTtl(long ttl)
    {
        this.ttl = ttl;
    }
}
